﻿namespace UnityEditor.Controls.ComponentControl
{
    using System;
    using System.ComponentModel;
    using System.Linq.Expressions;
    using System.Windows.Controls;
    using Microsoft.Xna.Framework;
    using System.Windows.Data;
    using System.Windows.Input;
    using UnityEditor.Layout;
    using Xceed.Wpf.Toolkit.PropertyGrid;

    /// <summary>
    /// Interaction logic for ComponentControl.xaml
    /// </summary>
    public partial class ComponentControl : UserControl, INotifyPropertyChanged
    {
        private UnityEngine.Core.Component component;

        public ComponentControl()
        {
            InitializeComponent();

            _propertyGrid.PropertyValueChanged +=
                new Xceed.Wpf.Toolkit.PropertyGrid.PropertyValueChangedEventHandler((o, e) =>
            {
                var target = o as dynamic;
            });

            collapsed = true;
            PART_Body.Height = 0;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public UnityEngine.Core.Component Component
        {
            get
            {
                return this.component;
            }
            set
            {
                component = value;
                if (component != null)
                    _propertyGrid.SelectedObject = component;
                else
                    _propertyGrid.ClearValue(PropertyGrid.SelectedObjectProperty);
            }
        }

        private ICommand closeCommand = null;
        public ICommand CloseCommand
        {
            get
            {
                if (closeCommand == null)
                    closeCommand = new RelayCommand(
                        (p) => { this.Component.RemoveSelfFromGameObject(); Workspace.Instance.GameObject = Workspace.Instance.GameObject; },
                        (p) => { return true; }
            );
                return closeCommand;
            }
        }

        private bool collapsed = true;
        private ICommand collapseCommand = null;
        public ICommand CollapseCommand
        {
            get
            {
                if (collapseCommand == null)
                    closeCommand = new RelayCommand(
                        (p) =>
                        {
                            if (collapsed)
                            {
                                collapsed = false;
                                PART_Body.Height = float.NaN;
                            }
                            else
                            {
                                collapsed = true;
                                PART_Body.Height = 0;
                            };
                        },
                        (p) => { return true; }
            );
                return closeCommand;
            }
        }


        public void NotifyPropertyChanged<TProperty>(Expression<Func<TProperty>> property)
        {
            var lambda = (LambdaExpression)property;
            MemberExpression memberExpression;
            if (lambda.Body is UnaryExpression)
            {
                var unaryExpression = (UnaryExpression)lambda.Body;
                memberExpression = (MemberExpression)unaryExpression.Operand;
            }
            else
                memberExpression = (MemberExpression)lambda.Body;

            this.OnPropertyChanged(memberExpression.Member.Name);
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
